# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2013 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2013
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------

class Cartasur::PostsController < Cartasur::ApplicationController
  include Geokit::Geocoders

  def new
   @post= Cartasur::Post.new
   @topic = Cartasur::Topic.find(params[:topic_id])
   @post.reply_post_id = params[:format] unless params[:format].nil?

   render 'cartasur/forums/topics/posts/new'
  end


  def create
      @post = Cartasur::Post.new(:content => ERB::Util.html_escape(params[:cartasur_post][:content]), :topic_id => params[:cartasur_post][:topic_id], :user_id => current_user.id, :reply_post_id => params[:cartasur_post][:reply_post_id] )
    if @post.save
      flash[:notice] = t 'cartasur.forums.posts.created'
      redirect_to "/cartasur/forums/#{params[:forum_id]}/topics/#{@post.topic_id}"
    else
      render :action => 'new'
    end
  end

  def edit
    @post  = Cartasur::Post.find(params[:id])
  end

  def update
    @post = Cartasur::Post.find(params[:id])

    if @forum.update_attributes(params[:cartasur_post])
      flash[:notice] = t 'cartasur.messages.successfullyupdated'
      redirect_to @post.topic
    else
      flash[:alert] = t 'cartasur.messages.problemupdating'
    end
  end

  def destroy
    @forum = Cartasur::Forum.find(params[:forum_id])
    @topic = Cartasur::Topic.find(params[:topic_id])
      post = Cartasur::Post.find(params[:id])
      if post.destroy
        flash[:notice] = t 'cartasur.forums.posts.postdeleted'
        redirect_to cartasur_forum_topic_url(@forum, @topic)
      end
  end

end
